package com.sinodata.bsm.center.engine.event.correlation;

import java.util.*;

import com.sinodata.bsm.common.vo.Event;
import com.sinodata.bsm.common.vo.EventCorrelationPolicy;

/**
 * 
 * <p>
 * Description:策略实例类，保存策略各分组信息 
 * </p>
 *
 * @author liulibo
 * @version 1.0

 * <p>
 * History: 
 *
 * Date                     Author         Version     Description
 * ---------------------------------------------------------------------------------
 * 2012-6-14 PM 9:39:26     liulibo        1.0         To create
 * </p>
 *
 * @since 
 * @see
 */
public class ECPInstance {

    //实例标识
    String id;

    EventCorrelationPolicy ecp;

    Event mainEvent;

    boolean saved = false;

    long createTime = System.currentTimeMillis();

    List<Set<Long>> expressList = null;

    public List<Set<Long>> getExpressList() {
        return expressList;
    }

    private List<Map<Long, List<Event>>> results = new ArrayList<Map<Long, List<Event>>>();

    public void setExpressList(List<Set<Long>> expressList) {
        this.expressList = expressList;
        clearSubEvent();
    }

    private boolean hasSubEvent = false;

    public void addSubEvent(Event evt) {
        for (Map<Long, List<Event>> map : results) {
            if (map.containsKey(evt.getEventTypeId().longValue())) {
                List<Event> events = map.get(evt.getEventTypeId().longValue());
                for (int i = events.size() - 1; i >= 0; i--) {
                    if (events.get(i).getStatus() > 1) {
                        events.remove(i);
                    }
                }
                events.add(evt);
            }
        }
    }

    public void removeConfirmdEvent() {
        for (Map<Long, List<Event>> map : results) {
            for (List<Event> events : map.values()) {
                for (int i = events.size() - 1; i >= 0; i--) {
                    if (events.get(i).getStatus() > 1) {
                        events.remove(i);
                    }
                }
            }
        }
    }

    public boolean hasSubEvent() {
        for (Map<Long, List<Event>> map : results) {
            for (List<Event> events : map.values()) {
                if (events.size() > 0) {
                    return true;
                }
            }
        }
        return false;
    }

    public void clearSubEvent() {
        for (int i = 0; i < expressList.size(); i++) {
            Map<Long, List<Event>> map = new HashMap<Long, List<Event>>();
            Set<Long> set = expressList.get(i);
            for (Long evtype : set) {
                map.put(evtype.longValue(), new ArrayList<Event>());
            }
            results.add(map);
        }
    }

    public long getCreateTime() {
        return createTime;
    }

    public boolean isSaved() {
        return saved;
    }

    public void setSaved(boolean saved) {
        this.saved = saved;
    }

    public EventCorrelationPolicy getEcp() {
        return ecp;
    }

    public void setEcp(EventCorrelationPolicy ecp) {
        this.ecp = ecp;
    }

    public Event getMainEvent() {
        return mainEvent;
    }

    public void setMainEvent(Event mainEvent) {
        this.mainEvent = mainEvent;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public List<Map<Long, List<Event>>> getResults() {
        return results;
    }

}
